<?php
/**
 * 服务对象
 */
namespace Core\Lib;
final class Service {
    /**
     * 服务实例
     * @var type 
     */
    private static $_instance;
    
    /**
     * 服务对象ID
     * @var string
     */
    private $service_id = null;
    
    /**
     * 服务对象
     * @var instance 
     */
    private $service = false;
    
    private function __construct($service) {
        // 服务ID
        if(!isset($service['name'])) trigger_error("Server name required!", E_USER_ERROR);
        $this->service_id = $service['name'];       
        
        // 服务实例
        if(!isset($service['class'])) trigger_error("Server class required!", E_USER_ERROR);
        $this->service = new $service['class'](isset($service['argv']) ? $service['argv'] : '');           
    }
    
    public function __clone(){
        trigger_error("Clone is not allow!", E_USER_WARNING);
    }
    
    /**
     * 实例化服务
     * @param array $service        服务配置信息
     * @return instance
     */
    public static function instance($service){
        if(!self::$_instance instanceof self){
            self::$_instance = new self($service);
        }
        
        return self::$_instance;
    }
    
    /**
     * 获取服务对象ID
     * @return string
     */
    public function getServiceId(){
        return $this->service_id;
    }
    
    /**
     * 获取服务对象
     * @return instance
     */
    public function getServiceInstance(){
        return $this->service;
    }
}